package com.jsmile.cloud.tradecenter.order.repo;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.tradecenter.order.dao.OrderAddressDao;
import com.jsmile.cloud.tradecenter.order.model.OrderAddress;
import com.jsmile.mall.api.enums.JSmileCacheEnum;

@Component
@Transactional(readOnly = true)
public class OrderAddressSearchRepository extends ServiceImpl<OrderAddressDao, OrderAddress> {

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'address_'+#orderNo")
    public List<OrderAddress> findByOrderNo(Long orderNo) {
        return this.baseMapper.selectList(new QueryWrapper<OrderAddress>().lambda().eq(OrderAddress::getOrderNo, orderNo));
    }

    public OrderAddress findOneByOrderNo(Long orderNo) {
        List<OrderAddress> orderAddresses = findByOrderNo(orderNo);
        if (!CollectionUtils.isEmpty(orderAddresses)) {
            return orderAddresses.get(0);
        }
        return null;
    }
}
